package com.example.demo.niuke;

/**
 * @Author: liull
 * @Description: 字符串指定位置反转 两边都包含
 * @Date: Created in 18:01 2020/3/28
 * @Modified By:
 */
public class Main37 {
    public static void main(String[] args) {
        System.out.println(reverse("abcdedg", 2, 5)); // abedcdg
    }

    public static String reverse(String s, int start, int end) {
        if (s == null || s.length() == 0) {
            return null;
        }
        int len = s.length();
        if (start < 0 || end > s.length()) {
            return null;
        }
        String prefix = s.substring(0, start);
        String subfix = s.substring(end + 1, len);
        // 反转部分
        String middle = s.substring(start, end + 1);
        StringBuilder sb = new StringBuilder(middle);
        return prefix + sb.reverse().toString() + subfix;
    }
}
